package com.bike.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.HhDiningStyle;
import com.bike.mapper.HhDiningStyleMapper;
import com.bike.util.PageData;
import com.bike.util.TransFerUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class HhDiningStyleServiceImpl extends BaseServcieImpl  {

	@Autowired
	HhDiningStyleMapper hhDiningStyleMapper;
	
	/**
	* 保存
	*/
	public void saveOrUpdate(PageData pd, HhDiningStyle record) {
		if(pd.getLong("diningStyleId")>0){
			 //编辑
			hhDiningStyleMapper.updateByPrimaryKeySelective(record);
		}else{
			hhDiningStyleMapper.insertSelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteHhDiningStyleById(long diningStyleId) {
		return hhDiningStyleMapper.deleteByPrimaryKey(diningStyleId);
	}
	
	/**
	*列表分页
	*/
	public PageData selectHhDiningStyleList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
		String advanceFilter=pd.getString("advanceFilter");
		if(advanceFilter!=null&&!advanceFilter.equals("")){
			advanceFilter=advanceFilter.replaceAll("'", "");
			JSONArray array=JSONObject.parseArray(advanceFilter);
			StringBuffer buffer=new StringBuffer();
			for(int i=0;i<array.size();i++){
				JSONObject object=array.getJSONObject(i);
				String field=object.getString("field");
				String op=object.getString("op");//equal  contains  notequal  greater  greaterorequal  less  lessorequal  beginwith  endwith
				String value=object.getString("value");
				buffer.append(" and ").append(TransFerUtil.getMybatisField(field)).append(TransFerUtil.getMybatisOperation(op,value));
			}
			if(array.size()>0){
				pd.put("condition", buffer.toString());
			}
		}
		List<HhDiningStyle> records= hhDiningStyleMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<HhDiningStyle>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<HhDiningStyle> listAll(PageData pd) {
		return  hhDiningStyleMapper.selectAll(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public HhDiningStyle selectByHhDiningStyleId(Long diningStyleId) {
		return hhDiningStyleMapper.selectByPrimaryKey(diningStyleId);
	}
}

